import "react-app-polyfill/stable";
import "react-app-polyfill/ie11";
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import {HashRouter as Router, Switch, Route, Redirect} from 'react-router-dom';
import 'antd/dist/antd.css'
import {mainRoutes} from './routes';
import {getToken} from './util/login'
import {ConfigProvider} from "antd";
import API from "./api/api";
let flush = window.onload = function() {

    if(localStorage.getItem("token")){
        API.loginToken()
    }

};

window.onbeforeunload = onbeforeunload_handler;
// window.onunload = onunload_handler;
function onbeforeunload_handler(){
    localStorage.setItem("hasExit",JSON.stringify(true))
}

ReactDOM.render(
    <Router>
        <Switch>
            {flush()}
            <Route path="/eip"  render={routeProps => getToken(routeProps) ?
                <ConfigProvider locale={"zh_CN"}>
                    <App {...routeProps}></App>
                </ConfigProvider>
                :
                <Redirect to="/Login"></Redirect>}>
            </Route>
            {mainRoutes.map(route => {
                return <Route key={route.path} {...route}></Route>
            })}
            <Redirect to="/eip/index" from="/"></Redirect>
            <Redirect to="/404"></Redirect>
        </Switch>
    </Router>,
    document.getElementById('root')
);

// serviceWorker.unregister();
